package com.atguigu.dga.meta.service.impl;

import com.atguigu.dga.constant.DgaConstant;
import com.atguigu.dga.meta.bean.TableMetaInfo;
import com.atguigu.dga.meta.bean.TableMetaInfoExtra;
import com.atguigu.dga.meta.mapper.TableMetaInfoExtraMapper;
import com.atguigu.dga.meta.service.TableMetaInfoExtraService;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 元数据表附加信息 服务实现类
 * </p>
 *
 * @author tubage
 * @since 2024-03-15
 */
@Service
@DS("dga")
public class TableMetaInfoExtraServiceImpl extends ServiceImpl<TableMetaInfoExtraMapper, TableMetaInfoExtra> implements TableMetaInfoExtraService {
    /**
     * 初始化表的辅助信息，每张表的辅助信息只需要初始化一次， 主要是给一些默认值， 未来，可以从页面中对表的辅助信息进行修改。
     * @param tableMetaInfoList
     */
    @Override
    public void initTableMetaInfoExtra(List<TableMetaInfo> tableMetaInfoList) {
        ArrayList<TableMetaInfoExtra> tableMetaInfoExtras = new ArrayList<>(tableMetaInfoList.size());

        for (TableMetaInfo tableMetaInfo : tableMetaInfoList) {
            // 查询当前表是否已经被初始化过
            TableMetaInfoExtra tableMetaInfoExtra = getOne(
                    new QueryWrapper<TableMetaInfoExtra>()
                            .eq("schema_name", tableMetaInfo.getSchemaName())
                            .eq("table_name", tableMetaInfo.getTableName())
            );

            if(tableMetaInfoExtra == null) {
                // 初始化表的辅助信息
                tableMetaInfoExtra =  new TableMetaInfoExtra() ;
                tableMetaInfoExtra.setSchemaName(tableMetaInfo.getSchemaName());
                tableMetaInfoExtra.setTableName(tableMetaInfo.getTableName());
                //技术负责人
                tableMetaInfoExtra.setTecOwnerUserName(DgaConstant.TEC_OWNER_UNSET );
                //业务负责人
                tableMetaInfoExtra.setBusiOwnerUserName( DgaConstant.BUSI_OWNER_UNSET);

                //生命周期类型
                tableMetaInfoExtra.setLifecycleType( DgaConstant.LIFECYCLE_TYPE_UNSET);
                //生命周期天数
                tableMetaInfoExtra.setLifecycleDays(-1L);

                //安全级别
                tableMetaInfoExtra.setSecurityLevel( DgaConstant.SECURITY_LEVEL_UNSET );

                //数仓层级
                tableMetaInfoExtra.setDwLevel( getTableLevelByName( tableMetaInfo.getTableName()));

                //创建时间
                tableMetaInfoExtra.setCreateTime(new Date());

                tableMetaInfoExtras.add(tableMetaInfoExtra);
            }
        }

        saveBatch(tableMetaInfoExtras);
    }

    private String getTableLevelByName(String tableName) {
        if(tableName.startsWith("ods")){
            return DgaConstant.DW_LEVEL_ODS;
        } else if (tableName.startsWith("dwd")) {
            return DgaConstant.DW_LEVEL_DWD;
        }else if (tableName.startsWith("dim")) {
            return DgaConstant.DW_LEVEL_DIM;
        }else if (tableName.startsWith("dws")) {
            return DgaConstant.DW_LEVEL_DWS;
        }else if (tableName.startsWith("ads")) {
            return DgaConstant.DW_LEVEL_ADS;
        }else if (tableName.startsWith("dm")) {
            return DgaConstant.DW_LEVEL_DM;
        }else  {
            return DgaConstant.DW_LEVEL_OTHER;
        }
    }

}
